{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf781a18",
   "metadata": {},
   "source": [
    "# 6.1 数值计算Nump"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6721a82f",
   "metadata": {},
   "source": [
    "### 两种基本的对象：\n",
    "* **ndarray(N-dimensional array object)和ufunc(universal function object)**\n",
    "1. ndarray(下文统称为数组)是存储单一数据类型的多维数组\n",
    "2. ufunc则是能够对数组进行处理的函数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a45c11a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3,4])#从列表创建\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "74ed7d0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array((5,6,7,8))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "839e1b64",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 7,  8,  9, 10]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#二位数组\n",
    "c = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc763ec0",
   "metadata": {},
   "source": [
    "### n.dim:秩，即轴的数量或维度的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "af9b118b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndim a: 1 ,b: 1 ,c: 2\n"
     ]
    }
   ],
   "source": [
    "print(\"ndim\",\"a:\",a.ndim,\",b:\",b.ndim,\",c:\",c.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dee033d",
   "metadata": {},
   "source": [
    "### .shape:ndarray对象的尺度，对于矩阵，n行m列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9bb8953c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shap a: (4,) ,b: (4,) ,c: (3, 4)\n"
     ]
    }
   ],
   "source": [
    "print(\"shap\",\"a:\",a.shape,\",b:\",b.shape,\",c:\",c.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc682926",
   "metadata": {},
   "source": [
    "### .size:ndarray对象元素的个数，相当于.shape中n*m的值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9e667b61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size a: 4 ,b: 4 ,c: 12\n"
     ]
    }
   ],
   "source": [
    "print(\"size\",\"a:\",a.size,\",b:\",b.size,\",c:\",c.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a42d1ebe",
   "metadata": {},
   "source": [
    "### .dtype:ndarray对象的元素类型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bcdc3e4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dtype a: int64 ,b: int64 ,c: int64\n"
     ]
    }
   ],
   "source": [
    "print(\"dtype\",\"a:\",a.dtype,\",b:\",b.dtype,\",c:\",c.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f5cf545",
   "metadata": {},
   "source": [
    "### .itemsize:ndarray对象中每个元素的大小，以字节为单位\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f6c2c167",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "itemsize a: 8 ,b: 8 ,c: 8\n"
     ]
    }
   ],
   "source": [
    "print(\"itemsize\",\"a:\",a.itemsize,\",b:\",b.itemsize,\",c:\",c.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3693a8a1",
   "metadata": {},
   "source": [
    "## 6.1.1 numpy的ndarray对象创建\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "031c6ea3",
   "metadata": {},
   "source": [
    "### ndarray实例方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f4356e",
   "metadata": {},
   "source": [
    "* tolist:将数组变回list，原数组不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b176301e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1677a9ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e96d3a94",
   "metadata": {},
   "source": [
    "* reshape:创建一个新尺寸数组，原数组的shape不变\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "35d93c9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4,  4,  5],\n",
       "       [ 6,  7,  7],\n",
       "       [ 8,  9, 10]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape=4,3\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e15d9672",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = a.reshape((2,2))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "95c39086",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b041220c",
   "metadata": {},
   "source": [
    "### 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "faaed230",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,1,0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a43176f9",
   "metadata": {},
   "source": [
    "### 创建特定数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "81a9fbab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((2,3))#2*3矩阵，元素全为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ac5bb0da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3))#2*3矩阵，元素全为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e7e6fc32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0.],\n",
       "       [0., 1., 0., 0.],\n",
       "       [0., 0., 1., 0.],\n",
       "       [0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(4) #4*4单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b8b1fb0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0, 0],\n",
       "       [0, 2, 0, 0, 0],\n",
       "       [0, 0, 3, 0, 0],\n",
       "       [0, 0, 0, 4, 0],\n",
       "       [0, 0, 0, 0, 5]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag([1,2,3,4,5])#5*5的对角矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14206b1f",
   "metadata": {},
   "source": [
    "### np.random模块(随机抽样)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52759204",
   "metadata": {},
   "source": [
    "1. 简单的随机数据生成函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5991e6bd",
   "metadata": {},
   "source": [
    "2. np.random模块（随机抽样）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ca1423cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 1, 2, 9, 6, 0, 7, 8, 3, 5])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shuffle(x):重排\n",
    "arr = np.arange(10)\n",
    "np.random.shuffle(arr)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "91e2bef0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 7, 9, 8, 5, 3, 4, 0, 2, 6], dtype=int32)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#permutation(x):返回一个随机排列\n",
    "np.random.permutation(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb7df025",
   "metadata": {},
   "source": [
    "### 概率分布"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6684f5d",
   "metadata": {},
   "source": [
    "## 6.1.2数组元素索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66104d49",
   "metadata": {},
   "source": [
    "1. 使用下标或切片\n",
    "2. 使用整数序列\n",
    "3. 使用布尔数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "54f248a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [10, 11, 12, 13, 14, 15],\n",
       "       [20, 21, 22, 23, 24, 25],\n",
       "       [30, 31, 32, 33, 34, 35],\n",
       "       [40, 41, 42, 43, 44, 45],\n",
       "       [50, 51, 52, 53, 54, 55]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range(start, stop) 生成从 start 到 stop-1 的连续整数序列。\n",
    "a = np.array([range(10*i, 10*i+6) for i in range(6)])\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1e2b8d",
   "metadata": {},
   "source": [
    "### 元素访问\n",
    "1. a[(0,1,2,3,4),(1,2,3,4,5)] : 用于存取数组的下标仍然是一个有两个元素的组元，组元中的每个元素都是整数序列，分别对应数组的第0轴和第1轴。从两个序列的对应位置取出两个整数组成下标： a[0,1], a[1,2], ..., a[4,5]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ace8f837",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 12, 23, 34, 45])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[(0,1,2,3,4),(1,2,3,4,5)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "683c15b4",
   "metadata": {},
   "source": [
    "2. a[3:, [0, 2, 5]] : 下标中的第0轴是一个范围，它选取第3行之后的所有行；第1轴是整数序列，它选取第0, 2, 5三列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d09e7531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[30, 32, 35],\n",
       "       [40, 42, 45],\n",
       "       [50, 52, 55]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3:,[0,2,5]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da5ad240",
   "metadata": {},
   "source": [
    "3. a[mask, 2] : 下标的第0轴是一个布尔数组，它选取第0，2，5行；第1轴是一个整数，选取第2列。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72cbed92",
   "metadata": {},
   "source": [
    "### axis参数\n",
    "* axis参数实际上指定索引序：第一个索引是axis=0，第二个索引是axis=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c0977551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [10, 11, 12, 13, 14, 15]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([range(10*i, 10*i+6) for i in range(2)])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "48061197",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 12, 14, 16, 18, 20])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "34f7a68c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([15, 75])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bd2c8da",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
